﻿/*******************************************************************
 * 作者：JOHN
 * 类型：添加页面
 * 时间：2011-11-25
 * 说明：
 * ****************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using HZ.Supervise.Model.Enum;

namespace HZ.Supervise.Web.Secondphase.NewReport
{
    public partial class HosPitalGoods_TotalByQT : BasePage
    {
        public HosPitalGoods_TotalByQT()
        {
            this.ModuleNo = Module.医院药品分类汇总统计.ToString();
            this.PageOperateType = OperateType.其他医疗机构.ToString();
        }
        HZ.Supervise.IBLL.IExecuteContext i = HZ.Supervise.BLLFactory.ExecuteContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.AreaControl1.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;

                BindData();
            }
        }


        public void BindData()
        {
            #region 查询方法

            int t = 0;

            StringBuilder sb = new StringBuilder();

            string orderby = this.OrderFileds.SelectedValue + "  " + this.OrderType.SelectedValue;//排序字段与方式

            if (!string.IsNullOrEmpty(AreaControl1.SelectedAreaID) && AreaControl1.SelectedAreaID != "510000")
            {
                if (AreaControl1.SelectedAreaID.Length == 4)
                {
                    sb.AppendFormat(" AND areaid like '{0}%'", AreaControl1.SelectedAreaID);
                }
                else
                {
                    sb.AppendFormat(" AND areaid ='{0}'", AreaControl1.SelectedAreaID);
                }
            }

            if (!string.IsNullOrEmpty(txtHospital.Text))
            {
                sb.AppendFormat(" AND HospitalName LIKE '%{0}%'", this.txtHospital.Text);
            }

            string timeFrom = string.Intern(this.txtTimeFrom.Text);

            string timeTo = string.Intern(this.txtTimeTo.Text);

            if (!string.IsNullOrEmpty(timeFrom))
            {
                sb.AppendFormat(" AND CenSubmiitTime >= '{0}'", timeFrom);
            }

            if (!string.IsNullOrEmpty(timeTo))
            {
                sb.AppendFormat(" AND CenSubmiitTime <= '{0}'", timeTo);
            }
            #endregion

            System.Data.DataTable dt = i.GetHospitalGoodsList(4, sb.ToString(), orderby, this.AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);

            System.Data.DataTable dtSum = i.GetHospitalGoodsList(4, sb.ToString(), orderby, 1, 0, out t);

            AspNetPager1.RecordCount = t;
            gvwInfo.PageSize = AspNetPager1.PageSize;
            this.gvwInfo.DataSource = dt;
            HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");
            HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate");
            HZ.Supervise.Web.Function.ChangeRate_Sell(dt, "SellRate");
            this.gvwInfo.DataBind();

            double PurchaseAmountTotal = 0;
            double WareHouseAmountTotal = 0;
            double DistributeAmountTotal = 0;
            double SellAmountTotal = 0;
            for (int j = 0; j < dtSum.Rows.Count; j++)
            {
                PurchaseAmountTotal += Convert.ToDouble(dtSum.Rows[j][4]);
                WareHouseAmountTotal += Convert.ToDouble(dtSum.Rows[j][7]);
                DistributeAmountTotal += Convert.ToDouble(dtSum.Rows[j][8]);
                SellAmountTotal += Convert.ToDouble(dtSum.Rows[j][11]);
            }

            this.lblCount.Text = "采购总金额为：" + PurchaseAmountTotal.ToString("0.000") + ";" + "入库总金额：" + WareHouseAmountTotal.ToString("0.000") + ";" + "配送总金额：" + DistributeAmountTotal.ToString("0.000") + ";" + "销售总金额：" + SellAmountTotal.ToString("0.000") + ";";
        }

        /// <summary>
        /// 翻页显示
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }


        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
        }


        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            #region 查询方法

            int t = 0;

            StringBuilder sb = new StringBuilder();

            string orderby = this.OrderFileds.SelectedValue + "  " + this.OrderType.SelectedValue;//排序字段与方式

            if (!string.IsNullOrEmpty(AreaControl1.SelectedAreaID) && AreaControl1.SelectedAreaID != "510000")
            {
                if (AreaControl1.SelectedAreaID.Length == 4)
                {
                    sb.AppendFormat(" AND areaid like '{0}%'", AreaControl1.SelectedAreaID);
                }
                else
                {
                    sb.AppendFormat(" AND areaid ='{0}'", AreaControl1.SelectedAreaID);
                }
            }

            if (!string.IsNullOrEmpty(txtHospital.Text))
            {
                sb.AppendFormat(" AND HospitalName LIKE '%{0}%'", this.txtHospital.Text);
            }

            string timeFrom = string.Intern(this.txtTimeFrom.Text);

            string timeTo = string.Intern(this.txtTimeTo.Text);

            if (!string.IsNullOrEmpty(timeFrom))
            {
                sb.AppendFormat(" AND CenSubmiitTime >= '{0}'", timeFrom);
            }

            if (!string.IsNullOrEmpty(timeTo))
            {
                sb.AppendFormat(" AND CenSubmiitTime <= '{0}'", timeTo);
            }

            #endregion

            System.Data.DataTable dt = i.GetHospitalGoodsList(4, sb.ToString(), orderby, 1, 0, out t);

            HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");
            HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate");
            HZ.Supervise.Web.Function.ChangeRate_Sell(dt, "SellRate");

            if (dt.Rows.Count <= 0)
            {
                HZ.Web.WebUtil.Alert("没有需要导出的数据！", this);
            }
            else
            {
                HZ.Excel.ExcelHelper.ExportDataForWeb(dt, gvwInfo);
            }
        }
    }
}